package com.cocotingo.snail.text;

import java.awt.Color;
import java.awt.Font;

import com.cocotingo.snail.Vector;
import com.cocotingo.snail.View;

public class GroupedRichTextView extends View {
	
	private Color defaultColor;
	private Font defaultFont;
	private RichTextParagraph[] paragraphs;
	private RichTextView[] richTextViews;
	private int preferredHeight;

	public GroupedRichTextView() {
		this(0, 0, 0, 0);
	}
	
	public GroupedRichTextView(int left, int top, int width, int height) {
		super(left, top, width, height);
		defaultColor = Color.black;
		defaultFont = TextView.DEFAULT_FONT;
		paragraphs = null;
		richTextViews = null;
		preferredHeight = 0;
	}
	
	public Color getDefaultColor() {
		return defaultColor;
	}
	
	public Font getDefaultFont() {
		return defaultFont;
	}
	
	public GroupedRichTextView setDefaultColor(Color defaultColor) {
		this.defaultColor = defaultColor;
		relayoutRichTextViews();
		return this;
	}
	
	public GroupedRichTextView setDefaultFont(Font defaultFont) {
		this.defaultFont = defaultFont;
		relayoutRichTextViews();
		return this;
	}
	
	@Override
	public View setBackgroundColor(Color backgroundColor) {
		super.setBackgroundColor(backgroundColor);
		relayoutRichTextViews();
		return this;
	}
	
	private void relayoutRichTextViews() {
		if (paragraphs != null && richTextViews != null) {
			int cur_top = 0;
			for (int i = 0; i < richTextViews.length; i++) {
				RichTextView view = richTextViews[i];
				RichTextParagraph p = paragraphs[i];
				view.setBackgroundColor(getBackgroundColor());
				view.setDefaultColor(defaultColor);
				view.setDefaultFont(defaultFont);
				view.setLineSpace(p.getLineSpace());
				view.setWidth(getWidth() - p.getLeftMargin() - p.getRightMargin());
				view.setHeight(view.getPreferredHeight());
				view.setPosition(p.getLeftMargin(), cur_top + p.getTopMargin());
				cur_top += p.getTopMargin() + view.getHeight() + p.getBottomMargin();
			}
			preferredHeight = cur_top;
		} else {
			preferredHeight = 0;
		}
	}
	
	@Override
	public View setSize(int width, int height) {
		super.setSize(width, height);
		relayoutRichTextViews();
		return this;
	}
	
	private boolean antialiased = true;
	
	public boolean isAntialiased() {
		return antialiased;
	}
	
	public void setAntialiased(boolean antialiased) {
		this.antialiased = antialiased;
		if (this.richTextViews != null) {
			for (RichTextView view : richTextViews) {
				view.setAntialiased(antialiased);
			}
		}
	}
	
	public GroupedRichTextView setText(RichTextParagraph[] paragraphs) {
		if (this.richTextViews != null) {
			for (RichTextView richTextView : richTextViews) {
				richTextView.removeFromSuperView();
			}
		}
		this.paragraphs = new RichTextParagraph[paragraphs.length];
		this.richTextViews = new RichTextView[paragraphs.length];
		for (int i = 0; i < paragraphs.length; i++) {
			this.paragraphs[i] = new RichTextParagraph((RichTextBlock[]) null);
			this.paragraphs[i].setLeftMargin(paragraphs[i].getLeftMargin());
			this.paragraphs[i].setRightMargin(paragraphs[i].getRightMargin());
			this.paragraphs[i].setTopMargin(paragraphs[i].getTopMargin());
			this.paragraphs[i].setBottomMargin(paragraphs[i].getBottomMargin());
			this.richTextViews[i] = new RichTextView(0, 0, getWidth() - paragraphs[i].getLeftMargin() - paragraphs[i].getRightMargin(), 0);
			addSubview(this.richTextViews[i], i);
			this.richTextViews[i].setLinebreakMode(paragraphs[i].getLinebreakMode());
			this.richTextViews[i].setText(paragraphs[i].getText());
			this.richTextViews[i].setAntialiased(antialiased);
			this.richTextViews[i].setHeight(this.richTextViews[i].getPreferredHeight());
		}
		relayoutRichTextViews();
		return this;
	}
	
	public GroupedRichTextView setText(GroupedRichTextView view) {
		return setText(view.paragraphs);
	}
	
	public int getPreferredHeight() {
		return preferredHeight;
	}
	
	public int testHitHotspot(Vector v) {
		int hotspot_index_offset = 0;
		for (int i = 0; i < richTextViews.length; i++) {
			RichTextView view = richTextViews[i];
			if (v.y >= view.getTop() && v.y <= view.getTop() + view.getHeight()) {
				Vector v1 = new Vector(v.x - view.getLeft(), v.y - view.getTop());
				int index = view.testHitHotspot(v1);
				if (index != -1) return index + hotspot_index_offset;
				else return -1;
			} else {
				hotspot_index_offset += view.getHotspotCount();
			}
		}
		return -1;
	}
	
	public int getHotspotCount() {
		int hotspot_index_offset = 0;
		for (int i = 0; i < richTextViews.length; i++) {
			RichTextView view = richTextViews[i];
			hotspot_index_offset += view.getHotspotCount();
		}
		return hotspot_index_offset;
	}
	
	public Hotspot getHotspot(int index) {
		int hotspot_index_offset = 0;
		for (int i = 0; i < richTextViews.length; i++) {
			RichTextView view = richTextViews[i];
			if (index < hotspot_index_offset + view.getHotspotCount()) {
				Hotspot hotspot = view.hotspots.get(index - hotspot_index_offset);
				Rectangle rect = hotspot.getFrame();
				rect.position.increase(view.getPosition());
				return new Hotspot(rect, hotspot.getText(), hotspot.getFont(), hotspot.getColor());
			} else {
				hotspot_index_offset += view.getHotspotCount();
			}
		}
		return null;
	}
	
	@Override
	public String toString() {
		StringBuffer buf = new StringBuffer();
		if (richTextViews != null) {
			for (RichTextView view : richTextViews) {
				buf.append(view);
				buf.append("\n");
			}
		}
		return buf.toString();
	}

}
